package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.Organization;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.entity.TrackDetail;
import com.xmy.cultivate.mapper.OrganizationMapper;
import com.xmy.cultivate.mapper.StaffMapper;
import com.xmy.cultivate.mapper.TrackDetailMapper;
import com.xmy.cultivate.service.ITrackDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author min
 * @since 2022-12-27
 */
@Service
public class TrackDetailServiceImpl extends ServiceImpl<TrackDetailMapper, TrackDetail> implements ITrackDetailService {

    @Autowired
    TrackDetailMapper trackDetailMapper;

    @Autowired
    StaffMapper staffMapper;

    @Autowired
    OrganizationMapper organizationMapper;

    public IPage<TrackDetail> findAll(IPage<TrackDetail> page, Wrapper wrapper){
        IPage<TrackDetail> trackDetailIPage = trackDetailMapper.selectPage(page,wrapper);

        for (TrackDetail trackDetail:trackDetailIPage.getRecords()){
            if(trackDetail.getType().equals(1)){
                Staff staff = staffMapper.selectById(trackDetail.getSourseId());
                if(staff !=null){
                    trackDetail.setName(staff.getName());
                }
            }else {
                Organization organization = organizationMapper.selectById(trackDetail.getSourseId());
                if(organization != null){
                    trackDetail.setName(organization.getName());
                }
            }
        }

        return trackDetailIPage;
    }
}
